#
# Copyright (C) [2024] Xingyun Integrated Circuit, Inc.
#
# GreenCode was a private technology asset of Xingyun Integrated Circuit， Inc （Confidential）
# Author: Shawn.Tan
# Date: 2025.10.28
#
# History: Initial Version 2025.10.28
#
#
from gpgpu.EnvGPGPU import EnvGPGPU
from gpgpu.GenThreadGPGPU import GenThreadGPGPU
from base.Sequence import Sequence


class MainSequence(Sequence):
    """Exercise different combinations of values for the parameters for
    the genPA instruction. Focus in this test is to try values of the Size,
    Align and CanAlias parameters. Type is always 'D'; Bank is always
    "Default".
    """

    def generate(self, **kargs):

        ldstr_byte_ops = ["LB##CALM", "SB##CALM"]
        ldstr_half_ops = ["LH##CALM", "SH##CALM"]
        ldstr_word_ops = ["LW##CALM", "SW##CALM"]
        ldstr_double_ops = ["LD##CALM", "SD##CALM"]

        theType = "D"
        theBank = "Default"
        theCanAlias = 0
        loopCount = 1

        # Iterate through Size and Align values.  Force requires Align to be
        # a power of 2. This 1st block tests smaller values of size -
        # 1 byte to 32 bytes.
        for theSize in [2 ** x for x in range(0, 5)]:

            for theAlign in [2 ** x for x in range(0, 8)]:

                if theAlign < theSize:
                    continue

                for _ in range(loopCount):

                    rand_PA = self.genPA(
                        Size=theSize,
                        Align=theAlign,
                        Type=theType,
                        Bank=theBank,
                        CanAlias=theCanAlias,
                    )
                    rand_VA = self.genVAforPA(
                        PA=rand_PA,
                        Bank=theBank,
                        FlatMap=0,
                        Type=theType,
                        Size=theSize,
                    )
                    self.notice(
                        ">>>>>> Requested Alignment:  {:6d}     Requested "
                        "Size:  {:6d}     PA target= {:16X}     VA target= "
                        "{:16X}".format(theAlign, theSize, rand_PA, rand_VA)
                    )

                    instr_id = self.genInstruction(
                        self.choice(ldstr_byte_ops), {"LSTarget": rand_VA}
                    )

        # Iterate through Size and Align values.  Force requires Align to be
        # a power of 2.
        # This 2nd block tests larger values of size - 32K to 8M.
        for theSize in [2 ** x for x in range(15, 18)]:

            for theAlign in [2 ** x for x in range(15, 18)]:

                if theAlign < theSize:
                    continue

                for _ in range(loopCount):

                    rand_PA = self.genPA(
                        Size=theSize,
                        Align=theAlign,
                        Type=theType,
                        Bank=theBank,
                        CanAlias=theCanAlias,
                    )
                    rand_VA = self.genVAforPA(
                        PA=rand_PA,
                        Bank=theBank,
                        FlatMap=0,
                        CanAlias=0,
                        ForceNewAddress=1,
                        Type=theType,
                        Size=theSize,
                    )
                    self.notice(
                        ">>>>>> Requested Alignment:  {:6d}     Requested "
                        "Size:  {:6d}     PA target= {:16X}     VA target= "
                        "{:16X}".format(theAlign, theSize, rand_PA, rand_VA)
                    )

                    instr_id = self.genInstruction(
                        self.choice(ldstr_byte_ops), {"LSTarget": rand_VA}
                    )


MainSequenceClass = MainSequence
GenThreadClass = GenThreadGPGPU
EnvClass = EnvGPGPU
